#!/bin/bash

##mysql 数据库备份配置
mysqlIp='172.10.0.22'
mysqlUser="root"
mysqlDb='minisdk'
mysqlPassword="WDnPIKiKSrqLtUdc"
mysqlPort="3306"

fileName='minisdk'.`date '+%Y%m%d'`
dirName='/data/sdkbak'/`date '+%Y%m'`
bakPath='/data/sdkbak/'
logFile='/data/sdkbak/mysql_backup.log'
##开始备份数据库

echo "===========`date '+%Y-%m-%d %H:%M:%S'` 开始导出数据库===========" >> $logFile
/usr/bin/mysqldump --host="$mysqlIp" --port="$mysqlPort" --user="$mysqlUser" --password="$mysqlPassword" --opt --flush-logs --master-data=2 --single-transaction --default-character-set=utf8 $mysqlDb  > $bakPath/$fileName.sql
echo "===========`date '+%Y-%m-%d %H:%M:%S'` 导出完成，导出文件$fileName.sql===========" >> $logFile
echo "+++++++++++++++++++`date '+%Y-%m-%d %H:%M:%S'` 对数据库sql文件打包备份+++++++" >> $logFile
cd $bakPath

if [ ! -x "$dirName" ]; then
  mkdir "$dirName"
fi

tar -zcvf $fileName.tar.gz $fileName.sql

mv $fileName.tar.gz $dirName/$fileName.tar.gz
echo "+++++++++++++++ `date '+%Y-%m-%d %H:%M:%S'` 打包完成++++" >> $logFile
#echo "+++++++++++++++++++`date '+%Y-%m-%d %H:%M:%S'` 开始导入到测试机+++++++" >> $logFile
#mysql -uroot -pWDnPIKiKSrqLtUdc minisdk < $fileName

rm -rf $bakPath/$fileName.sql